package com.szxb.facebus.task.bindservice;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.lilei.tool.tool.IToolInterface;
import com.szxb.facebus.intrefaces.OnIInterface;
import com.szxb.facebus.util.log.MLog;
import com.szxb.q8server.MyTool;

/**
 * 作者：Tangren on 2018-11-24
 * 包名：com.szxb.jcbus.task.bindservice
 * 邮箱：996489865@qq.com
 * TODO:一句话描述
 */

public class BindService {

    private OnIInterface onIInterface;
    private Context mContext;
    private IToolInterface mIToolInterface;
    private MyTool mMyTool;

    public BindService(Context mContext) {
        this.mContext = mContext;
        bindSystemTool();
        bindTool();
    }

    class MyServiceConnection implements ServiceConnection {

        boolean isSystemTool;

        public MyServiceConnection(boolean isSystemTool) {
            this.isSystemTool = isSystemTool;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (isSystemTool) {
                mIToolInterface = IToolInterface.Stub.asInterface(service);
                try {
                    mIToolInterface.asBinder().linkToDeath(new MyDeathRecipient(isSystemTool), 0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                if (onIInterface != null) {
                    onIInterface.setIInterface(mIToolInterface);
                }
            } else {
                mMyTool = MyTool.Stub.asInterface(service);
                try {
                    mMyTool.asBinder().linkToDeath(new MyDeathRecipient(isSystemTool), 0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                if (onIInterface != null) {
                    onIInterface.setIInterface(mMyTool);
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }


    class MyDeathRecipient implements IBinder.DeathRecipient {
        boolean isSystemTool;

        public MyDeathRecipient(boolean isSystemTool) {
            this.isSystemTool = isSystemTool;
        }

        @Override
        public void binderDied() {
            if (isSystemTool) {
                if (mIToolInterface == null) {
                    return;
                }
                mIToolInterface.asBinder().unlinkToDeath(this, 0);
                bindSystemTool();
            } else {
                if (mMyTool == null) {
                    return;
                }
                mMyTool.asBinder().unlinkToDeath(this, 0);
                bindTool();
            }
        }
    }


    private void bindTool() {
        Intent intent = new Intent();
        intent.setAction("com.cyd.tool");
        intent.setPackage("com.szxb.q8server");
        boolean bindRes = mContext.bindService(intent, new MyServiceConnection(false), Context.BIND_AUTO_CREATE);
        MLog.d("bindSystemTool(JCApp.java:231)系统Tool绑定结果=" + bindRes);

    }

    private void bindSystemTool() {
        Intent i = new Intent();
        i.setAction("com.lypeer.aidl");
        i.setPackage("com.lilei.tool.tool");
        boolean ret = mContext.bindService(i, new MyServiceConnection(true), Context.BIND_AUTO_CREATE);
        MLog.d("bindTool(JCApp.java:240)Q8Service绑定结果" + ret);
    }


    public void setOnIInterface(OnIInterface iInterface) {
        this.onIInterface = iInterface;
    }
}
